package com.dzx.permissionmanagersystem.common;

import com.dzx.permissionmanagersystem.entity.SysUser;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author DuanZhaoXu
 * @ClassName:
 * @Description:
 * @date 2018年11月30日 10:03:02
 */
@Component
@WebFilter(filterName = "loginFilter",urlPatterns = "/**")
public class LoginFilter  implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest  request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        SysUser  sysUser =  (SysUser) request.getSession().getAttribute("user");

        if(sysUser ==null){
          String  path =   "signin.html";
          return;
        }
        RequestHolder.add(sysUser);
        RequestHolder.add(request);
        filterChain.doFilter(request,response);
        return;
    }
}
